﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace pgen
{
    public class PinHistory
    {
        private readonly List<long> _history = new List<long>();

        public void LoadFromFile(string filename)
        {
            ClearHistory();
            if (!File.Exists(filename)) return;
            using (var reader = new BinaryReader(File.OpenRead(filename)))
            {
                var pos = 0;
                var len = (int) reader.BaseStream.Length;
                while (pos < len)
                {
                    _history.Add(reader.ReadInt64());
                    pos += sizeof (long);
                }
            }
        }

        public void SaveToFile(string filename)
        {
            using (var writer = new BinaryWriter(File.Open(filename, FileMode.Create)))
            {
                foreach (var l in _history)
                {
                    writer.Write(l);
                }
            }
        }

        public void ClearHistory()
        {
            _history.Clear();
        }

        public void AddToHistory(PinCode code)
        {
            _history.Add(code.ToLong());
        }

        public bool PinIsUnique(PinCode code)
        {
            return (_history.IndexOf(code.ToLong()) == -1);
        }
    }
}
